<?php

namespace Modules\Admin\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Routing\Controller;
use Modules\Admin\Http\Requests\RoleRequest;
use Spatie\Permission\Models\Role;
class RoleController extends Controller
{
    /**
     * Display a listing of the resource.
     * @return Response
     */
    public function index()
    {
    	$roles=Role::get();
        return view('admin::role.index',compact('roles'));
    }

    /**
     * Show the form for creating a new resource.
     * @return Response
     */
    public function create()
    {
        return view('admin::create');
    }

    /**
     * Store a newly created resource in storage.
     * @param  Request $request
     * @return Response
     */
    public function store(RoleRequest $request)
    {
		Role::create($request->all());
		session()->flash('success','添加角色成功！');
		return redirect ()->back();
    }

    /**
     * Show the specified resource.
     * @return Response
     */
    public function show()
    {
        return view('admin::show');
    }

    /**
     * Show the form for editing the specified resource.
     * @return Response
     */
    public function edit()
    {
        return view('admin::edit');
    }

    /**
     * Update the specified resource in storage.
     * @param  Request $request
     * @return Response
     */
    public function update(RoleRequest $request,$id)
    {
    	$role=Role::find($id);
    	$role->name=$request->input('name');
    	$role->title=$request->input('title');
    	$role->save();
    	session()->flash('success','角色编辑成功！');
    	return redirect ()->back();
    }

    /**
     * Remove the specified resource from storage.
     * @return Response
     */
    public function destroy($id)
    {
    	$role=Role::find($id);
    	if($role->name=='webmaster'){
    		session()->flash('danger','年轻人，你的思想很危险！');
    		return redirect ()->back();
		}

    	Role::destroy ($id);
    	session()->flash('success','删除角色成功！');
    	return redirect ()->back();
    }
    public function showpermission($id){
    	$role=Role::find($id);
		$permissions=\HDModule::getPermissionByGuard('admin');
		return view('admin::permission.showpermission',compact('permissions','role'));
	}
	public function addpermission(Request $request,$id){
		if(!$request->input('name')){
			session()->flash('danger','请至少选择一个权限！');
			return redirect ()->back();
		}
		$role=Role::find($id);
		$role->syncPermissions($request->input('name'));
		session()->flash ('success','权限更改成功！');
		return redirect ()->route('role.index');
	}
}
